/*******************************************************************************
 * Copyright (c) 2009 Paul VanderLei, Simon Archer, Jeff McAffer and others. All
 * rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Toast is an Equinox/OSGi system developed for the book Eclipse, Equinox and
 * OSGi - Creating Highly Modular Java Applications See http://equinoxosgi.org
 * 
 * Contributors: Paul VanderLei, Simon Archer and Jeff McAffer - initial API and
 * implementation
 *******************************************************************************/
package org.equinoxosgi.toast.internal.backend.emergency.bundle;

import org.equinoxosgi.toast.core.ICoreConstants;
import org.equinoxosgi.toast.core.LogUtility;
import org.equinoxosgi.toast.core.PropertyManager;
import org.equinoxosgi.toast.core.UrlBuilder;
import org.equinoxosgi.toast.core.emergency.IEmergencyConstants;
import org.equinoxosgi.toast.internal.backend.emergency.EmergencyServlet;
import org.osgi.service.http.HttpService;

public class Component {
	private HttpService http;
	private String servletAlias;

	public void setHttp(HttpService value) {
		http = value;
	}

	protected void shutdown() {
		http.unregister(servletAlias);
	}

	protected void startup() {
		try {
			String servletRoot = PropertyManager.getProperty(
					ICoreConstants.BACK_END_URL_PROPERTY,
					ICoreConstants.BACK_END_URL_DEFAULT);
			UrlBuilder urlBuilder = new UrlBuilder(servletRoot);
			urlBuilder.appendPath(IEmergencyConstants.EMERGENCY_FUNCTION);
			servletAlias = urlBuilder.getPath();

			EmergencyServlet servlet = new EmergencyServlet();
			http.registerServlet(servletAlias, servlet, null, null);
			LogUtility.logDebug(this, "Registered EmergencyServlet at "
					+ servletAlias);
		} catch (Exception e) {
			LogUtility.logError(this,
					"Error registering servlet with HttpService", e);
		}
	}
}
